package createModel.singletonMode.threadLocalSigleton;

public class TLocalSigleton {

    private String errorMsg;

    private String successMsg;

    private TLocalSigleton store;

    private TLocalSigleton(){

    }
    private static ThreadLocal<TLocalSigleton> threadLocal = new ThreadLocal<TLocalSigleton>();

    private static TLocalSigleton instance = null;


    public static TLocalSigleton getInstance(){
        if (instance == null){
            synchronized (TLocalSigleton.class){
                if (instance == null){
                    instance = new TLocalSigleton();
                    threadLocal.set(instance);
                }
            }
        }
        return instance;
    }

    public TLocalSigleton setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
        return this;
    }

    public TLocalSigleton setSuccessMsg(String successMsg) {
        this.successMsg = successMsg;
        return this;
    }

    /**
     * 功能描述: 复制一个,存储到线程本地变量
     *
     * @return {@link TLocalSigleton }
     */
    public TLocalSigleton store(){
        if (instance != null){
            this.store = instance;
            threadLocal.set(store);
        }
        return threadLocal.get();
    }

    public TLocalSigleton recall(){
        if (store !=null){
            threadLocal.set(store);
            store = null;
        }
        return threadLocal.get();
    }
    /**
     * 功能描述: 重置,清空这个对象
     *
     * @return {@link TLocalSigleton }
     */
    public TLocalSigleton reset(){
        errorMsg ="";
        successMsg = "";
        threadLocal.remove();
        return this;
    }
}
